using System;
using System.Collections.Generic;
using Plossum.CommandLine;
using CIP4.Common;

namespace CIP4.BusDriver
{
    [CommandLineManager(
        ApplicationName = BusDriverProgram.APPNAME,
        Description = "A commandline app",
        Copyright = "Copyright 2010 Channel Intelligence Inc")]
    class OptionsType
    {
        // plossum uses reflection to get to these variables, which the compiler can't see, so supppress never assigned warnings
#pragma warning disable 0649
        [CommandLineOption(Description = "help text",
            Aliases = "?", Prohibits = "Confirm")]
        public bool Help;

        [CommandLineOption(Description = "The string used to identify the bus that will carry the messages - This must be the same as the BusId property on the CIP4ConnectionAttribute used by the client to build the wrapper that talks to the bus on the client side",
            Aliases = "bid")]
        public string BusId { get; set; }

        [CommandLineOption(Description = "The path for the assembly that contains Item types that will be transported by the bus - any class with a CIP4ItemAttribute will be handled by the bus",
            Aliases = "ian")]
        public string ItemAssemblyName { get; set; }

        [CommandLineOption(Description = "The name of the CIP4 provider that will handle reception of the messages - the provider must implmenet a class called RequestMessageReceiver",
            Aliases = "pan")]
        public string ProviderAssemblyName { get; set; }

        public Dictionary<string, string> ConnectionProperties = new Dictionary<string, string>();

        public Dictionary<string, string> ConnectionStrings = new Dictionary<string, string>();

        [CommandLineOption(Description = "A name value pair giving a connection property value to be used by the BusDriver to configure the provider the bus driver uses - run CIP4.Instaler to see the currently available connection types and their properties",
            Aliases = "cp", MaxOccurs = 100)]
        public void ConnectionProperty(string namedValue)
        {
            string name = namedValue.Substring(0, namedValue.IndexOf(":"));
            string[] parts = name.Split(new[] {'.'});
            if (parts.Length != 2)
                throw new ArgumentException(string.Format("The connection property value is not valid it must be in the form \"<collection name>.<property name>:<value>\" - the provided value does not match this pattern: {0}", namedValue));
            string value = namedValue.Substring(name.Length + 1);
            if (string.IsNullOrEmpty(value))
                throw new ArgumentException(string.Format("The connection property value is not valid it must be in the form \"<collection name>.<property name>:<value>\" - the provided value does not match this pattern: {0}", namedValue));
            ConnectionProperties.Add(name, value);
            if (ConnectionStrings.ContainsKey(parts[0]))
                ConnectionStrings[parts[0]] += string.Format(";{0}={1}", parts[1], value);
            else
                ConnectionStrings.Add(parts[0], string.Format("{0}={1}", parts[1], value));
        }

        [CommandLineOption(Description = "Allows for multiple copies to be running at the same time",
            Aliases = "lfnq")]
        public string LogFileNameQualifier { get; set; }

        [CommandLineOption(Description = "Controls the level of logging - Noise is the most verbose, Error the least - you cannot turn off errors",
            Aliases = "l")]
        public MajorEnum LoggingLevel { get; set; }

        [CommandLineOption(Description = "Displays the current options and asks for confirmation before continuing",
            Aliases = "c")]
        public bool Confirm { get; set; }

#pragma warning restore 0649

        public override string ToString()
        {
            string text = string.Format("/BusId {0} /ItemAssemblyName {1} /ProviderAssemblyName {2} /LoggingLevel {3}",
                BusId, ItemAssemblyName, ProviderAssemblyName, LoggingLevel);
            foreach (KeyValuePair<string,string> kvp in ConnectionProperties)
                text += string.Format(" /ConnectionProperty \"{0}:{1}\"", kvp.Key, kvp.Value);
            return text;
        }
    }
}
